package View;

import DB.Evento.ControllerEvento;
import DB.Util;
import Model.Evento;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

public class EventoDetailView extends javax.swing.JFrame {

    Evento e;

    /**
     * Creates new form UsersView
     *
     * @param event
     */
    public EventoDetailView(Evento event) {
        e = event;

        initComponents();

        SimpleDateFormat df = Util.getFormatoData();

        textID.setText(String.valueOf(e.getId()));
        textNome.setText(e.getNome());
        textDescricao.setText(e.getDescricao());
        textValor.setText(String.valueOf(e.getValor()));
        textVagas.setText(String.valueOf(e.getVagas()));
        textDataEvento.setText(df.format(e.getDataEvento()));
        textInicioInscricoes.setText(df.format(e.getInicioInscricoes()));
        textFimInscricoes.setText(df.format(e.getFimInscricoes()));
        textEndereco.setText(e.getEndereco());
        textCidade.setText(e.getCidade());
        textEstado.setText(e.getEstado());
        textPais.setText(e.getPais());
        textCEP.setText(e.getCep());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btAtualizar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        textID = new javax.swing.JTextField();
        textNome = new javax.swing.JTextField();
        textValor = new javax.swing.JTextField();
        textVagas = new javax.swing.JTextField();
        textDataEvento = new javax.swing.JTextField();
        textFimInscricoes = new javax.swing.JTextField();
        labelID = new javax.swing.JLabel();
        labelNome = new javax.swing.JLabel();
        labelDescricao = new javax.swing.JLabel();
        labelValor = new javax.swing.JLabel();
        labelVagas = new javax.swing.JLabel();
        labelDataEvento = new javax.swing.JLabel();
        labelFimInscricoes = new javax.swing.JLabel();
        labelEndereco = new javax.swing.JLabel();
        labelCidade = new javax.swing.JLabel();
        labelPais = new javax.swing.JLabel();
        labelEstado = new javax.swing.JLabel();
        textCidade = new javax.swing.JTextField();
        textEndereco = new javax.swing.JTextField();
        textPais = new javax.swing.JTextField();
        textEstado = new javax.swing.JTextField();
        labelInicioInscricoes = new javax.swing.JLabel();
        textInicioInscricoes = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        textDescricao = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        textCEP = new javax.swing.JTextField();
        btParticipantes = new javax.swing.JButton();
        btMatricular = new javax.swing.JButton();

        setTitle("CADASTRO DE EVENTOS");
        setResizable(false);

        btAtualizar.setText("Atualizar");
        btAtualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAtualizarActionPerformed(evt);
            }
        });

        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        textID.setEditable(false);
        textID.setText("jTextField1");

        textNome.setText("jTextField2");

        textValor.setText("jTextField4");

        textVagas.setText("jTextField5");

        textDataEvento.setText("jTextField6");

        textFimInscricoes.setText("jTextField8");

        labelID.setText("ID:");

        labelNome.setText("Nome*:");

        labelDescricao.setText("Descrição*:");

        labelValor.setText("Valor*:");

        labelVagas.setText("Vagas*:");

        labelDataEvento.setText("Data evento (mm/dd/yyyy)*:");

        labelFimInscricoes.setText("Fim inscrições (mm/dd/yyyy)*:");

        labelEndereco.setText("Endereço:");

        labelCidade.setText("Cidade:");

        labelPais.setText("País:");

        labelEstado.setText("Estado:");

        textCidade.setText("jTextField3");

        textEndereco.setText("jTextField4");

        textPais.setText("jTextField6");

        textEstado.setText("jTextField11");

        labelInicioInscricoes.setText("Inicio inscrições (mm/dd/yyyy)*:");

        textInicioInscricoes.setText("jTextField1");

        textDescricao.setColumns(20);
        textDescricao.setRows(3);
        jScrollPane1.setViewportView(textDescricao);

        jLabel1.setText("CEP:");

        textCEP.setText("jTextField1");

        btParticipantes.setText("Participantes...");
        btParticipantes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btParticipantesActionPerformed(evt);
            }
        });

        btMatricular.setText("Matricular...");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(labelEndereco)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textEndereco))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelInicioInscricoes)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textInicioInscricoes, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(labelFimInscricoes)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textFimInscricoes, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btParticipantes)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btMatricular)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btAtualizar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelCidade)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelPais)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textPais, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelEstado)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textCEP))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelID)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textID, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(labelNome)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textNome, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(labelDescricao)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(labelValor)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(textValor, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(labelVagas)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(textVagas)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(labelDataEvento)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(textDataEvento, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 562, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelID)
                    .addComponent(textID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelNome)
                    .addComponent(textNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelDescricao)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelValor)
                    .addComponent(textValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textVagas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelVagas)
                    .addComponent(labelDataEvento)
                    .addComponent(textDataEvento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelInicioInscricoes)
                    .addComponent(textInicioInscricoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelFimInscricoes)
                    .addComponent(textFimInscricoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelEndereco)
                    .addComponent(textEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCidade)
                    .addComponent(textCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPais)
                    .addComponent(textPais, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelEstado)
                    .addComponent(jLabel1)
                    .addComponent(textCEP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btAtualizar)
                    .addComponent(btCancelar)
                    .addComponent(btParticipantes)
                    .addComponent(btMatricular))
                .addContainerGap())
        );

        setSize(new java.awt.Dimension(598, 355));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btAtualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAtualizarActionPerformed
        if (textNome.getText().length() < 10 || textDescricao.getText().isEmpty() || textVagas.getText().isEmpty() || textDataEvento.getText().isEmpty() || textInicioInscricoes.getText().isEmpty() || textFimInscricoes.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Campos não preenchidos.", "Erro", JOptionPane.ERROR_MESSAGE);
        } else {
            e.setNome(textNome.getText());
            e.setDescricao(textDescricao.getText());
            
            try {
                e.setValor(Double.valueOf(textValor.getText()));
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null, "Valor invalido para o campo Valor.");
                return;
            }
            try {
                e.setVagas(Integer.valueOf(textVagas.getText()));
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(null, "Valor invalido para o campo Vagas.");
                return;
            }
            try {
                e.setDataEvento(new Date(textDataEvento.getText()).getTime());
            } catch (IllegalArgumentException ex) {
                JOptionPane.showMessageDialog(null, "Valor invalido para o campo Data.");
                return;
            }
            try {
                e.setInicioInscricoes(new Date(textInicioInscricoes.getText()).getTime());
            } catch (IllegalArgumentException ex) {
                JOptionPane.showMessageDialog(null, "Valor invalido para o campo Inicio Inscrições.");
                return;
            }
            try {
                e.setFimInscricoes(new Date(textFimInscricoes.getText()).getTime());
            } catch (IllegalArgumentException ex) {
                JOptionPane.showMessageDialog(null, "Valor invalido para o campo Fim Inscrições.");
                return;
            }
            
            e.setEndereco(textEndereco.getText());
            e.setCidade(textCidade.getText());
            e.setEstado(textEstado.getText());
            e.setPais(textCidade.getText());
            e.setCep(textEstado.getText());

            ControllerEvento ce = ControllerEvento.getInstance();
            long result = ce.update(e);

            if (result > 0) {
                JOptionPane.showMessageDialog(null, "O evento foi atualizado.");
            }

            this.setVisible(false);
        }
    }//GEN-LAST:event_btAtualizarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btParticipantesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btParticipantesActionPerformed
        EventoParticipantes ep = new EventoParticipantes(e);
        ep.setVisible(true);
    }//GEN-LAST:event_btParticipantesActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAtualizar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btMatricular;
    private javax.swing.JButton btParticipantes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelCidade;
    private javax.swing.JLabel labelDataEvento;
    private javax.swing.JLabel labelDescricao;
    private javax.swing.JLabel labelEndereco;
    private javax.swing.JLabel labelEstado;
    private javax.swing.JLabel labelFimInscricoes;
    private javax.swing.JLabel labelID;
    private javax.swing.JLabel labelInicioInscricoes;
    private javax.swing.JLabel labelNome;
    private javax.swing.JLabel labelPais;
    private javax.swing.JLabel labelVagas;
    private javax.swing.JLabel labelValor;
    private javax.swing.JTextField textCEP;
    private javax.swing.JTextField textCidade;
    private javax.swing.JTextField textDataEvento;
    private javax.swing.JTextArea textDescricao;
    private javax.swing.JTextField textEndereco;
    private javax.swing.JTextField textEstado;
    private javax.swing.JTextField textFimInscricoes;
    private javax.swing.JTextField textID;
    private javax.swing.JTextField textInicioInscricoes;
    private javax.swing.JTextField textNome;
    private javax.swing.JTextField textPais;
    private javax.swing.JTextField textVagas;
    private javax.swing.JTextField textValor;
    // End of variables declaration//GEN-END:variables
}
